package SwingController;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

import JDBC.CmsDatabaseConnection;
import Main.Main;
import SwingModel.CmsCourse;
import SwingModel.CmsInstructor;
import SwingModel.CmsStudent;
import SwingModel.CmsUser;
import SwingView.CmsAccountPanel;
import SwingView.CmsCPCourse;
import SwingView.CmsCPHome;
import SwingView.CmsCPPeople;
import SwingView.CmsContentPanel;
import SwingView.CmsFeaturePanel;
import SwingView.CmsLoginView;
import SwingView.CmsMainView;
import SwingView.CmsView;
import SwingView.Announcement.ALAnnouncement;
import SwingView.Assignment.ALAssignment;
import SwingView.CmsPageSelector.CmsPage;
import SwingView.Gradebook.ALGradebook;
import SwingView.Lecture.ALLecture;
import SwingView.Lecture.ALLecturePost;
import SwingView.Message.ALMessage;
import SwingView.Quiz.ALQuiz;
import SwingView.Quiz.ALQuizComposer;


public class CmsPresenter {
	
	public static final Date DEFAULT_AVAI_DATE = new Date(73627200000l);
	public static final Date DEFAULT_EXP_DATE = new Date(4129592400000l);
	public static final Date END_OF_SEMESTER = new Date(1387688400000l);
	public static final SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM dd YYYY,  hh:mma");
	public static final SimpleDateFormat sdfHour = new SimpleDateFormat("H");
	public static final SimpleDateFormat sdfMin = new SimpleDateFormat("m");
	
	
	public static CmsUser user = null;
	
	public static CmsLoginView loginView = null;
	public static CmsMainView mainView = null;

	
	//---------------------------------------------------------
	//INITIATE METHOD:
	//---------------------------------------------------------
	/**
	 * This method instantiate an instance of the loginView, add action
	 * listeners and display the view on the screen.
	 */
	public static void initiate(){
		
		//instantiate LoginView
		loginView = new CmsLoginView();
		
		//...........................................................................
		//ADD LISTENERS FOR LOGIN FRAMES:
		//quit button
		loginView.getJbtQuit().addActionListener(new ALQuit());
		
		//login button
		loginView.getJbtLogIn().addActionListener(new ALLogin());
		//...........................................................................
		
		//cmsLoginFrame.setVisible(true);
		loginView.setVisible(true);
	}
	
	//---------------------------------------------------------
	//INITIATE METHOD:
	//---------------------------------------------------------
	/**
	 * This method is called after the user is validated and authenticated
	 * <p>
	 * This method instantiate an instance of the mainView and its components 
	 * and add action listeners for for the components on the mainView. This 
	 * method then close the loginView and display the mainView.
	 * 
	 * @param user 
	 */
	public static void startMainView(CmsUser user){
		
		CmsPresenter.user = user;
		
		//user full name
		String fullName = user.getFirstName() + " " + user.getLastName();
		
		//user type:
		String userType;
		if(user instanceof CmsInstructor) userType = "Instructor";
		else userType = "Student";
		
		//instantiate mainView
		mainView = new CmsMainView(fullName,userType,user.getListOfCourse());
		
		//RETRIEVE COMPONENTS ON THE MAIN VIEW
		JButton logoutButton = mainView.getAccountPanel().getJbtLogout();
		JButton homeButton = mainView.getHomeButton();
		JButton announcementButton = mainView.getAnnouncementButton();
		JButton messageButton = mainView.getMessageButton();
		JButton quizButton = mainView.getQuizButton();
		JButton assignmentButton = mainView.getAssignmentButton();
		JButton peopleButton = mainView.getPeopleButton();
		JButton gradebookButton = mainView.getGradebookButton();
		JButton lectureButton = mainView.getLectureMaterialButton();
		JComboBox<String> courseComboBox = mainView.getAccountPanel().getJcbCourseList();
		
		//ADD ACTION LISTENERS FOR COMPONENTS ON THE MAIN VIEW
		logoutButton.addActionListener(new ALLogout(mainView, loginView, user));
		courseComboBox.addActionListener(new ALCourse(mainView,user));
		homeButton.addActionListener(new ALHome(mainView,user));
		announcementButton.addActionListener(new ALAnnouncement(mainView,user));
		messageButton.addActionListener(new ALMessage(mainView,user));
		quizButton.addActionListener(new ALQuiz());
		assignmentButton.addActionListener(new ALAssignment(mainView,user));
		peopleButton.addActionListener(new ALPeople(mainView,user));
		gradebookButton.addActionListener(new ALGradebook());
		lectureButton.addActionListener(new ALLecture());
		
		//ADD ACTION LISTENERS FOR LECTURE:
		mainView.getContentPanel().getLectureInstructorPage().addALLecturePost(new ALLecturePost());
		
		
		
		//DISPLAY MAIN VIEW
		mainView.setSize(loginView.getSize()); //ensure login frame and main frame as same size
		mainView.setLocation(loginView.getLocation());//ensure login frame and main frame as same location
		loginView.setVisible(false); //close login frame
		mainView.revalidate();
		mainView.setVisible(true);//open main frame
	}
	
}
